/**
 * 
 */
package com.chengyu.eyc.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chengyu.eyc.properties.WechatProperties;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kevin
 *
 */
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    WechatService wechatService;
    @Autowired
    WechatProperties wechatProperties;
    
    
    // { "key1": { "value": any }, "key2": { "value": any } }
    @Override
    public Boolean pushOrderPaySuccessMessage(String openid,  Map<String,Object> dataMap) {
        log.info("推送支付成功的消息给：{}",openid);
        String templateId=wechatProperties.getSubcribeMessage().getTemplateId();
        Map<String,Object> params=new HashMap<>();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("page", "pages/my/my");
        params.put("data", dataMap);
        params.put("miniprogram_state", "trial");
        log.info("参数：{}",params);
        String accessToken=wechatService.getAccessToken(templateId);
        String url=String.format("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s", accessToken);
        log.info("url:{}",url);
      
        Map<String, String> reqHeader = new HashMap<>();
        reqHeader.put("Content-Type", "application/json;charset=utf-8");
        JSONObject jsonObj=new JSONObject(params);
        String result= HttpRequest.post(url).addHeaders(reqHeader).body(jsonObj.toString())
        .execute().body();
//        String result= HttpUtil.post(url, params);
        log.info("推送订阅消息返回值：{}",result);
        @SuppressWarnings("unchecked")
        Map<String,Object> resultMap= JSONUtil.toBean(result, Map.class);
        return (int)resultMap.get("errcode")==0 ;
    }
    
    /**
     * @see com.chengyu.eyc.service.MessageService#pushMessage(java.lang.String)
     */
    @Override
    public Boolean pushOrderRefundSucceessMessage(String openid,  Map<String,Object> dataMap) {
        return pushTemplateMessage(openid,wechatProperties.getRefundMessage().getTemplateId(), dataMap);
    }

    @Override
    public Boolean pushOrderRejectRefundMessage(String openid,  Map<String,Object> dataMap) {
        return pushTemplateMessage(openid,wechatProperties.getRejectRefundMessage().getTemplateId(), dataMap);
    }
    
    /**
     * @param openid
     * @param dataMap
     * @return
     */
    private Boolean pushTemplateMessage(String openid,String templateId, Map<String, Object> dataMap) {
        String accessToken=wechatService.getAccessToken(openid);
        String url=String.format("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=%s", accessToken);
        Map<String,Object> params=new HashMap<>();
        params.put("touser", openid);
        Map<String,Object> tplParams=new HashMap<>();
        tplParams.put("appid", wechatProperties.getOfficialAccount().getAppId());
        tplParams.put("template_id",templateId);
        tplParams.put("url", "https://lichetang.com/");
        Map<String,Object> miniParams=new HashMap<>();
        miniParams.put("appid", wechatProperties.getAppId());
        miniParams.put("page", "page/index/index");
        tplParams.put("miniprogram",miniParams );
//        String data= new JSONObject(dataMap).toString();
        tplParams.put("data", dataMap);
        params.put("mp_template_msg", tplParams);
        Map<String, String> reqHeader = new HashMap<>();
        reqHeader.put("Content-Type", "application/json;charset=utf-8");
        JSONObject jsonObj=new JSONObject(params);
        log.info("参数：{}",params);
        String result= HttpRequest.post(url).addHeaders(reqHeader).body(jsonObj.toString())
        .execute().body();
        log.info("推送模版消息返回值：{}",result);
        @SuppressWarnings("unchecked")
        Map<String,Object> resultMap= JSONUtil.toBean(result, Map.class);
        return (int)resultMap.get("errcode")==0 ;
    }
    
  
}
